/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __packet_add.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/13 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

extern int __packet_add(Frbyte_Packet *p_packet, char *p_buf, unsigned int ui_buf_len);

static void case1(void)
{
    Frbyte_Packet *p_packet;
    int i_ret = 0;
    Frlist_node *p_node = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;
    char *p_buf = NULL;
    unsigned int ui_buf_len = 0;

    p_packet = bytebuff_creat();

    p_packet->pos_index = 0;
    p_packet->ui_all_len = 10;
    p_packet->ui_skip_len = 0;

    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));

    p_bytebuffer->p_buf = frstrdup("1234567890");
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;

    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    //运行
    p_buf = frstrdup("test123");
    ui_buf_len = 7;
    i_ret = __packet_add(p_packet, p_buf, ui_buf_len);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_STRING_EQUAL(p_bytebuffer_tmp->p_buf,"1234567890");
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,7);
        CU_ASSERT_NSTRING_EQUAL(p_bytebuffer_tmp->p_buf,"test123",7);
    }

    bytebuff_free(&p_packet);
    frfree(p_buf);
    return;
}


static void case2(void)
{
    Frbyte_Packet *p_packet;
    int i_ret = 0;
    Frlist_node *p_node = NULL;
    Frlist_node *p_node_tmp = NULL;
    Frbyte_buffer *p_bytebuffer = NULL;
    Frbyte_buffer *p_bytebuffer_tmp = NULL;
    char *p_buf = NULL;
    unsigned int ui_buf_len = 0;

    p_packet = bytebuff_creat();

    p_packet->pos_index = 0;
    p_packet->ui_all_len = 10;
    p_packet->ui_skip_len = 0;

    //添加第一个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));

    p_bytebuffer->p_buf = frstrdup("1234567890");
    p_bytebuffer->ui_len = 10;
    p_bytebuffer->pos = 0;

    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    //添加第2个节点
    p_bytebuffer = (Frbyte_buffer *)frmalloc(sizeof(Frbyte_buffer));

    p_bytebuffer->p_buf = NULL;
    p_bytebuffer->ui_len = 100;
    p_bytebuffer->pos = 0;

    p_node = frlist_node_create(p_bytebuffer);
    frlist_add_tail(p_packet->p_lst_bytebuffer, p_node);

    //运行
    p_buf = frstrdup("test123");
    ui_buf_len = 7;
    i_ret = __packet_add(p_packet, p_buf, ui_buf_len);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);
    //确认第一个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 0);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,10);
        CU_ASSERT_STRING_EQUAL(p_bytebuffer_tmp->p_buf,"1234567890");
    }
    //确认第二个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 1);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,100);
        CU_ASSERT_PTR_NULL(p_bytebuffer_tmp->p_buf);
    }
    //确认第三个节点
    p_node_tmp = frlist_node_idx(p_packet->p_lst_bytebuffer, 2);
    if (p_node_tmp && p_node_tmp->data)
    {
        p_bytebuffer_tmp = (Frbyte_buffer *)p_node_tmp->data;
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->pos,0);
        CU_ASSERT_EQUAL(p_bytebuffer_tmp->ui_len,7);
        CU_ASSERT_NSTRING_EQUAL(p_bytebuffer_tmp->p_buf,"test123",7);
    }

    bytebuff_free(&p_packet);
    frfree(p_buf);
    return;
}

int add_suite__packet_add(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frcomm __packet_add()", __suite_init, __suite_cleanup)) == NULL)
    return FR_FAIL;

    CU_add_test(pSuite, "case1", case1);
    CU_add_test(pSuite, "case2", case2);

    return FR_SUCC;
}
